#include <stdlib.h>

#include "pbthreads.h"
#include "../os/platform.h"


#ifdef USE_POSIX_THREADS
#define THREAD_RETURN       return
typedef void *THREAD_RETURN_TYPE;
#else
#define THREAD_RETURN       sizeof
typedef void THREAD_RETURN_TYPE;
#endif


static THREAD_RETURN_TYPE ThreadFunc(void *pThreadParam)
{
    // nem csinal semmit

    THREAD_RETURN(0);
}


static void StartThreads(int count, IResultWriter *results)
{   
    double start = os_GetTime();

    void *threads = os_CreateThreadVariables(count);

    if (!threads)
       return ;

    bool ok = true;

    try
    {
        for (int i=0; i<count; i++)
        {                
            os_StartThread(threads, i, ThreadFunc, (void*)i);
        }
    }
    catch (...)
    {
        ok = false;
    }

    double end = os_GetTime();

    for (int i=0; i<count; i++)
        os_JoinThread(threads, i);

    os_DeleteThreadVariables(threads, count);

    char buff[128];
    sprintf(buff, "start%d", count);
    results->WriteNode(buff, ok ? (float)(end - start) : 0.0f);
}


void CThreadsBenchmark::Run(IResultWriter *results, const CCmdlineArgs *args)
{
    StartThreads(300, results);
}

#undef THREAD_RETURN
